﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace TaxLawView
{
    public class ViewModel : INotifyPropertyChanged
    {
        public ObservableCollection<Category> Categories { get; private set; }
        private ObservableCollection<Law> _laws;
        public ObservableCollection<Law> Laws
        {
            get { return _laws; }
            set
            {
                if (!object.Equals(_laws, value))
                {
                    _laws = value;
                    OnPropertyChanged("Laws");
                    if (_laws != null)
                    {
                        CurrentLaw = _laws.FirstOrDefault();
                    }
                }
            }
        }
        private Category _currentCategory;
        public Category CurrentCategory
        {
            get { return _currentCategory; }
            set
            {
                _currentCategory = value;
                OnPropertyChanged("CurrentCategory");
                Laws = Service.GetLaws(value);
            }
        }
        private Law _currentLaw;
        public Law CurrentLaw
        {
            get { return _currentLaw; }
            set
            {
                if (null != value && _currentLaw != value)
                {
                    _currentLaw = value;
                    if (_currentLaw != null && _currentLaw.Content == null)
                    {
                        _currentLaw.LoadContent();
                    }
                    OnPropertyChanged("CurrentLaw");
                }
            }
        }
        public ViewModel()
        {
            this.Categories = Service.GetCategories(null);
            CurrentCategory = Categories.FirstOrDefault();
        }

        #region INotifyPropertyChanged 接口

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propName)
        {
            PropertyChangedEventHandler handler = this.PropertyChanged;
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propName));
        }

        #endregion
    }
}
